package com.inossem.warehousestorage_inossem.activity.receive;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.receive.extra.ReceiveExtra;
import com.inossem.warehousestorage_inossem.adapter.receive.ReceiverListAdapter;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveReceiveItemBean;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.ReceiveUtils;

public class ReceiveAddReceiverActivity extends BaseActivity {

    private static final String SUCCESS = "1";//成功

    private static final String FAIL = "0";//失败

    private static final boolean DRAFT = false;//草稿

    private static final boolean CONFIRM = true;//提交

    private TextView title, nextStep;

    private PullToRefreshLayout refresh;

    private PullableListView receiverList;

    private ReceiverListAdapter adapter;

    private boolean mIsInsert;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_receive_add_receiver);
        initView(ReceiveAddReceiverActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(ReceiveAddReceiverActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.receiverListRefresh);
        receiverList = (PullableListView) activity.findViewById(R.id.receiverList);
    }

    private void initData(Activity activity) {
        title.setText("验收经办人");
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (ReceiveUtils.hasThreeReceiver(activity)) {
                    Intent intent = new Intent(activity, ReceiveConfirmActivity.class);
                    intent.putExtra(ReceiveExtra.IS_INSERT, mIsInsert);
                    startActivity(intent);
                } else {
                    Utils.middleToast(activity, activity.getResources().getString(R.string.activity_receive_add_receiver_fill_in_three_receiver));
                }
            }
        });
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_receive_item_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        receiverList.setPullUpFlag(false);
        receiverList.setPullDownFlag(false);
        getData(activity);
        getExtra(activity);
    }

    private void getExtra(Activity activity) {
        mIsInsert = activity.getIntent().getBooleanExtra(ReceiveExtra.IS_INSERT, false);
    }

    private void getData(Context context) {
        ReceiveReceiveItemBean.DataBean data = ReceiveUtils.getReceiveReceiveItemBean(context).getData();
        addReceiverListData(context, data, receiverList);
    }

    private void addReceiverListData(Context context, ReceiveReceiveItemBean.DataBean data, ListView listView) {
        adapter = new ReceiverListAdapter(context, data, false, () -> {
            initData(ReceiveAddReceiverActivity.this);
        });
        listView.setAdapter(adapter);
    }

    public interface ReceiveAddReceiverListener {
        void onDeleteReceiverListener();
    }

}
